//
//  PriceQueryTableViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/7/27.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "PriceQueryTableViewController.h"
#import "PriceQueryCell.h"
#import "PriceQueryViewController.h"
#import "PriceQueryDetailViewController.h"
//#import "DevelopListModel.h"

@interface PriceQueryTableViewController ()

@property (nonatomic, strong)NSArray *jsonArray;

@end

static NSString *priceQueryCellIdentifier = @"PriceQueryCell";

@implementation PriceQueryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"PriceQueryCell" bundle:nil]
         forCellReuseIdentifier:priceQueryCellIdentifier];
    if (self.ifTab) {
        self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height - 47);
        self.navigationItem.leftBarButtonItem = nil;
    } else {
        [self leftButtonItem];
    }
    if (self.ifPriceQuery) {
        [self titleView:@"询价单列表"];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"新建询价" style:UIBarButtonItemStyleBordered target:self action:@selector(newPriceQuery)];
    } else {
        [self titleView:@"制版打样单"];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"新制版打样" style:UIBarButtonItemStyleBordered target:self action:@selector(newPriceQuery)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    [self.navigationController.navigationBar setTintColor:[UIUtil colorWithHexString:appWhiteColor]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"UINavigationbar"] forBarMetrics:UIBarMetricsDefault];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableView.header beginRefreshing];
}

- (void)requestData {
    if (![UserModel sharedInstance].userName) {
        [self.navigationController pushViewController:[LoginViewController new] animated:YES];
        return;
    }
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":@"0"};
    if (self.ifPriceQuery) {
        [RequestData priceQueryListParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
            if (error) {
                [self.tableView.header endRefreshing];
                if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                    [UIUtil tokenOverdue:self msg:msg];
                } else {
                    [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
                }
            } else {
                [self.tableView.header endRefreshing];
                self.jsonArray = jsonArray;
                [self.tableView reloadData];
            }
        }];
    } else {
        [RequestData developListParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
            if (error) {
                [self.tableView.header endRefreshing];
                if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                    [UIUtil tokenOverdue:self msg:msg];
                } else {
                    [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
                }
            } else {
                [self.tableView.header endRefreshing];
                self.jsonArray = jsonArray;
                [self.tableView reloadData];
            }
        }];
    }
}

- (void)leftButtonItem {
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [backButton addTarget:self action:@selector(backToPrevious) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton_back = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"whiteBackIcon"]];
    imageView.frame=CGRectMake(0, 4, 14, 20);
    [backButton addSubview:imageView];
    [self.navigationItem setLeftBarButtonItem:barButton_back];
}

- (void) backToPrevious {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)newPriceQuery {
    PriceQueryViewController *view = [[PriceQueryViewController alloc] init];
    if (self.ifPriceQuery) {
        view.priceTitle = @"询价单";
    } else {
        view.priceTitle = @"制版打样";
    }
    view.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:view animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jsonArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PriceQueryCell *cell = [tableView dequeueReusableCellWithIdentifier:priceQueryCellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[PriceQueryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:priceQueryCellIdentifier];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PriceQueryDetailViewController *view = [[PriceQueryDetailViewController alloc] init];
    view.hidesBottomBarWhenPushed = YES;
    view.ifPriceQuery = self.ifPriceQuery;
    view.needId = self.ifPriceQuery ? ((PriceQueryModel *)[self.jsonArray objectAtIndex:indexPath.row]).inquireId:((PriceQueryModel *)[self.jsonArray objectAtIndex:indexPath.row]).needId;
    [self.navigationController pushViewController:view animated:YES];
}


@end
